package com.zys.el.user.controller;

import com.zys.el.core.controller.BaseController;
import com.zys.el.core.po.ResponseBean;
import com.zys.el.user.config.po.UserProfession;
import com.zys.el.user.po.Prossion;
import com.zys.el.user.po.User;
import com.zys.el.user.service.IProssionService;
import com.zys.el.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@RestController
@RequestMapping(value = "/prossion")
public class ProssionController extends BaseController<IProssionService, Prossion> {

    @Autowired
    private IProssionService service;
    @Autowired
    private IUserService userService;

//    @GetMapping("/{userId}")
//    public Map<String, List<Long>> getCitiesByUserId(@PathVariable Long userId) {
//        return service.getSchoolNameByUserId(userId);
//    }

    @GetMapping("/user/{userId}")
    public ResponseBean getUserProfessionByUserId(@PathVariable Long userId) {
        Prossion schoolOneNameByUserId = service.getSchoolOneNameByUserId(userId);
        User byId = userService.getById(userId);
        UserProfession moodel = UserProfession.builder().profession(schoolOneNameByUserId).user(byId).build();

        ResponseBean rm = new ResponseBean();
        rm.setMsg("查询成功");
        rm.setModel(moodel);
        rm.setSuccess(true);

        return rm;
    }

    /**
     * 获取所有省份
     *
     * @return
     */
    @GetMapping("/get_provinces")
    public ResponseBean getProvinceList() {
        List<String> provinceList = service.getProvinceList();
        ResponseBean rm = new ResponseBean();
        rm.setMsg("获取成功");
        rm.setModel(provinceList);
        return rm;
    }

    /**
     * 通过省份名称获取所有城市
     *
     * @param provinceName
     * @return
     */
    @GetMapping("/get_cities/{provinceName}")
    public ResponseBean getCityByProvinceName(@PathVariable String provinceName) {
        List<String> cityByProvinceName = service.getCityByProvinceName(provinceName);
        ResponseBean rm = new ResponseBean();
        rm.setMsg("获取成功");
        rm.setModel(cityByProvinceName);
        return rm;
    }

    /**
     * 通过城市名称获取所有学校
     *
     * @param cityName
     * @return
     */
    @GetMapping("/get_schools/{cityName}")
    public ResponseBean getSchoolByCityName(@PathVariable String cityName) {
        List<String> schoolByCityName = service.getSchoolByCityName(cityName);
        ResponseBean rm = new ResponseBean();
        rm.setMsg("获取成功");
        rm.setModel(schoolByCityName);
        return rm;
    }

    /**
     * 通过学校名称获取所有院
     *
     * @param schoolName
     * @return
     */
    @GetMapping("/get_types/{schoolName}")
    public ResponseBean getProfessionTypeBySchoolName(@PathVariable String schoolName) {
        List<String> professionTypeBySchoolName = service.getProfessionTypeBySchoolName(schoolName);
        ResponseBean rm = new ResponseBean();
        rm.setMsg("获取成功");
        rm.setModel(professionTypeBySchoolName);
        return rm;
    }

    /**
     * 通过院名称和学校名称获取所有专业
     *
     * @param professionType
     * @param schoolName
     * @return
     */
    @GetMapping("/get_professions/{schoolName}/{professionType}")
    public ResponseBean getProfessionByTypeAndSchoolName(@PathVariable String professionType, @PathVariable String schoolName) {
        List<String> professionByTypeAndSchoolName = service.getProfessionByTypeAndSchoolName(professionType, schoolName);
        ResponseBean rm = new ResponseBean();
        rm.setMsg("获取成功");
        rm.setModel(professionByTypeAndSchoolName);
        return rm;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @PostMapping("/save_profession/{userId}/{userAge}")
    public ResponseBean saveProfession(@RequestBody Prossion prossion,
                                       @PathVariable Long userId,
                                       @PathVariable Integer userAge) {
        Prossion one = service.query()
                .eq("province", prossion.getProvince())
                .eq("city", prossion.getCity())
                .eq("school", prossion.getSchool())
                .eq("prossion_type", prossion.getProssionType())
                .eq("prossion_name", prossion.getProssionName()).one();
        ResponseBean rm = new ResponseBean();
        if (one == null) {
            rm.setCode(500);
            rm.setSuccess(false);
            rm.setMsg("保存失败");
        } else {
            User user = userService.getById(userId);
            user.setAge(userAge);
            if (user == null) {
                rm.setCode(500);
                rm.setSuccess(false);
                rm.setMsg("用户不存在");
            } else {
                if (user.getProfessionId() == null) {
                    user.setProfessionId(one.getId());
                    userService.saveOrUpdate(user);
                    rm.setCode(200);
                    rm.setSuccess(true);
                    rm.setMsg("保存成功");
                } else {
                    userService.saveOrUpdate(user);
                    rm.setCode(200);
                    rm.setSuccess(true);
                    rm.setMsg("更改成功");
                }
            }


        }
        return rm;
    }

}
